﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using log4net;
using OpelWelcomeScreenWeb.NetUtils.Settings;
using OpelWelcomeScreenWeb.NetUtils;
using OpelWelcomeScreenWeb.Models;
using nsBaseClass;

namespace OpelWelcomeScreenWeb
{
    public partial class _Default : System.Web.UI.Page
    {
        protected static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        protected void Page_Load(object sender, EventArgs e)
        {

            // Demo get Settings
            //var site = AppConfig.Get(Constants.DEFAULT_SITE);
            var dayNames = AppConfig.GetOpelWelcomeSettings("dbconfigs");
            //foreach (var key in dayNames.Keys)
            //{
            //    var value = dayNames.Get(key as String);
            //    var settings = key + ":" + value;
            //    logger.Debug(settings);
            //}
            

            //longtest
            var Settings = AppConfig.GetWebAppSettings();

            string path = Page.Server.MapPath("Assets/images");
           // logger.Debug(path);
            string site;
            string overdue;
            string aftertime;
            int counter;
            string sessionID;


            if (String.IsNullOrEmpty(Page.Request.QueryString["sessionID"]))
                sessionID = "";
            else
                sessionID = Page.Request.QueryString["sessionID"];

           

            if (String.IsNullOrEmpty(Page.Request.QueryString["site"]))
                site = Settings["DEFAULT_SITE"];
            else
                site = Page.Request.QueryString["site"];
            if (!String.IsNullOrEmpty(Page.Request.QueryString["overdue"]))
                overdue = Page.Request.QueryString["overdue"];
            else
                overdue = Settings["DEFAULT_OVERDUE"];

            string temp = Page.Request.QueryString["counter"];
            if (!Int32.TryParse(temp, out counter))
                counter = 0;

            if (!String.IsNullOrEmpty(Page.Request.QueryString["aftertime"]))
                aftertime = Page.Request.QueryString["aftertime"];
            else
                aftertime = Settings["DEFAULT_AFTERTIME"];

            logger.Debug("Connect to Database");
            this.connectDb(sessionID);

            //Services ser = new Services();
            //Hashtable ht = new Hashtable();

            //ht = ser.getPictures(counter, site, overdue, aftertime, path);


           
        }
        public void connectDb(string sessionID) 
        {
            
            var objGlobal = new clsGlobalVariable();
            if (String.IsNullOrEmpty(sessionID))
            {
                var Settings = AppConfig.GetWebAppSettings();
                objGlobal.InitFromCommandLine(
                    Settings["DMSDBName"],
                    Settings["WinAut"],
                    Settings["DMSUserName"],
                    Settings["DMSDBPass"],
                    Settings["Site"]
                );
            }
            else
            {
                DbConfigSection config = new DbConfigSection();
                config = ConfigurationManager.GetSection("dbConfigs") as DbConfigSection;
                foreach (DBConfig cf in config.dbConfigs)
                {
                    //logger.Debug(cf.sessionID);
                    //logger.Debug(cf.Site); 
                    if (cf.sessionID.Equals(sessionID))
                    {
                        objGlobal.InitFromCommandLine(cf.DMSDBName, cf.WinAut, cf.DMSUserName, cf.DMSDBPass, cf.Site);
                        break;
                    }
                }
            }

            
        }
    }
}
